﻿using Avalonia.Controls.Primitives;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;

namespace AvaloniaApplication1.Controls
{
    public class TestThump : Thumb
    {
        /// <summary>
        /// 测试用 移动量
        /// </summary>
        private double m_DeltaVertical=-1;

        /// <summary>
        /// 测试用 移动量
        /// </summary>
        private double m_DeltaHorizontal = -1;

        /// <summary>
        /// 拉伸的角度
        /// </summary>
        private double angle;

        private RelativePoint transformOrigin;
        private Control designerItem;

        public TestThump()
        {
            DragStarted += ResizeThumb_DragStarted;
            DragDelta += TestThump_DragDelta;
            DragCompleted += TestThump_DragCompleted;
            //designerItem.ActualHeight
            


        }

        /// <summary>
        /// 画完了
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TestThump_DragCompleted(object sender, Avalonia.Input.VectorEventArgs e)
        {
            StringBuilder sbValue = new StringBuilder();
            sbValue.AppendLine($"{m_DeltaVertical}");
            sbValue.AppendLine($"{m_DeltaHorizontal}");
        }

        private void TestThump_DragDelta(object sender, Avalonia.Input.VectorEventArgs e)
        {
            if (this.designerItem != null)
            {
                //WPF的 horizontalChange和verticalChange 对应e.Vector.X和e.Vector.Y
                double horizontalChange = e.Vector.X;
                double verticalChange = e.Vector.Y;
                //改变的尺寸
                double deltaVertical, deltaHorizontal;

                switch (VerticalAlignment)
                {
                    case Avalonia.Layout.VerticalAlignment.Bottom:
                        //
                        deltaVertical = Math.Min(-verticalChange, this.designerItem.Height - this.designerItem.MinHeight);
                        Canvas.SetTop(this.designerItem, Canvas.GetTop(this.designerItem) + (this.transformOrigin.Point.Y * deltaVertical * (1 - Math.Cos(-this.angle))));
                        Canvas.SetLeft(this.designerItem, Canvas.GetLeft(this.designerItem) - deltaVertical * this.transformOrigin.Point.Y * Math.Sin(-this.angle));
                        //修改尺寸
                        this.designerItem.Height -= deltaVertical;
                        break;
                    case Avalonia.Layout.VerticalAlignment.Top:
                        deltaVertical = Math.Min(verticalChange, this.designerItem.Height - this.designerItem.MinHeight);
                        Canvas.SetTop(this.designerItem, Canvas.GetTop(this.designerItem) + deltaVertical * Math.Cos(-this.angle) + (this.transformOrigin.Point.Y * deltaVertical * (1 - Math.Cos(-this.angle))));
                        Canvas.SetLeft(this.designerItem, Canvas.GetLeft(this.designerItem) + deltaVertical * Math.Sin(-this.angle) - (this.transformOrigin.Point.Y * deltaVertical * Math.Sin(-this.angle)));
                        this.designerItem.Height -= deltaVertical;
                        break;
                    default:
                        break;
                }

                switch (HorizontalAlignment)
                {
                    case Avalonia.Layout.HorizontalAlignment.Left:
                        deltaHorizontal = Math.Min(horizontalChange, this.designerItem.Width - this.designerItem.MinWidth);
                        Canvas.SetTop(this.designerItem, Canvas.GetTop(this.designerItem) + deltaHorizontal * Math.Sin(this.angle) - this.transformOrigin.Point.X * deltaHorizontal * Math.Sin(this.angle));
                        Canvas.SetLeft(this.designerItem, Canvas.GetLeft(this.designerItem) + deltaHorizontal * Math.Cos(this.angle) + (this.transformOrigin.Point.X * deltaHorizontal * (1 - Math.Cos(this.angle))));
                        this.designerItem.Width -= deltaHorizontal;
                        break;
                    case Avalonia.Layout.HorizontalAlignment.Right:
                        deltaHorizontal = Math.Min(-horizontalChange, this.designerItem.Width - this.designerItem.MinWidth);
                        Canvas.SetTop(this.designerItem, Canvas.GetTop(this.designerItem) - this.transformOrigin.Point.X * deltaHorizontal * Math.Sin(this.angle));
                        Canvas.SetLeft(this.designerItem, Canvas.GetLeft(this.designerItem) + (deltaHorizontal * this.transformOrigin.Point.X * (1 - Math.Cos(this.angle))));
                        this.designerItem.Width -= deltaHorizontal;
                        break;
                    default:
                        break;
                }
            }

            e.Handled = true;
        }

        /// <summary>
        /// 角度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ResizeThumb_DragStarted(object sender, Avalonia.Input.VectorEventArgs e)
        {
            this.designerItem = DataContext as Control;
            if (designerItem!=null)
            {
                this.transformOrigin = this.designerItem.RenderTransformOrigin;
                RotateTransform rotateTransform = this.designerItem.RenderTransform as RotateTransform;
                if (rotateTransform != null)
                {
                    this.angle = rotateTransform.Angle * Math.PI / 180.0;
                }
                else
                {
                    this.angle = 0;
                }
            }
        }
    }
}
